﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ZenSystemService.Api.Business;
using ZenSystemService.Api.Common.Auth;
using ZenSystemService.Api.DTOs;
using ZenSystemService.Api.Entities;
using ZenSystemService.Api.Services;

namespace ZenSystemService.Api.Controllers
{
    /// <summary>
    /// 设备类型接口
    /// </summary>
    [Route("api/deviceTypes")]
    [ApiController]
    //[Authorize(Roles = "Admin")] // 只允许Admin角色访问
    public class DeviceTypeController : ControllerBase
    {
        private readonly DeviceService _deviceTypeService;

        public DeviceTypeController(DeviceService deviceTypeService)
        {
            _deviceTypeService = deviceTypeService;
        }

        /// <summary>
        /// 得到所有设备类型
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        //[FunctionAuthorize("GetAllDeviceTypes")]
        public async Task<ReturnObjectT<IEnumerable<DeviceType>>> GetAllDeviceTypes(ReceiveObject _receiveObject)
        {
            ReturnObjectT<IEnumerable<DeviceType>> _returnObject=new ReturnObjectT<IEnumerable<DeviceType>>();
      

            var deviceTypes = await _deviceTypeService.GetAllDeviceTypesAsync();
            _returnObject.Code = 1;
            _returnObject.Data = deviceTypes;
            return _returnObject;
           // return Ok(deviceTypes);
        }

        /// <summary>
        /// 根据ID 获取设备类型实体信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("GetDeviceTypeById")]
        public async Task<ReturnObjectT<DeviceType>> GetDeviceTypeById(ReceiveObjectT<int> _receiveObjectT)
        {
            ReturnObjectT<DeviceType> _returnObject = new ReturnObjectT<DeviceType>();

            int id = _receiveObjectT.Data;
            var deviceType = await _deviceTypeService.GetDeviceTypeByIdAsync(id);
            if (deviceType == null)
            {
                //return NotFound();
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = "获取设备类型信息失败";
            }
            else
            {
                _returnObject.Code = 1;
                _returnObject.Data = deviceType;
            }
            return _returnObject;
            // return Ok(deviceType);
        }

        /// <summary>
        /// 创建设备类型信息
        /// </summary>
        /// <param name="_receiveObjectT"></param>
        /// <returns></returns>
        [HttpPost("CreateDeviceType")]
        public async Task<ReturnObjectT<DeviceType>> CreateDeviceType(ReceiveObjectT<DeviceType> _receiveObjectT)
        {
            ReturnObjectT<DeviceType> _returnObject = new ReturnObjectT<DeviceType>();
   
            DeviceType deviceType = _receiveObjectT.Data;
            bool isSuccess= _deviceTypeService.CreateDeviceTypeAsync(ref deviceType);
            if (isSuccess)
            {
                _returnObject.Code = 1;
                _returnObject.Data = deviceType;
            }
            else
            {
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = "创建设备类型信息失败";
            }
            return _returnObject;
            //return CreatedAtAction(nameof(GetDeviceTypeById), new { id = deviceType.DeviceTypeId }, deviceType);
        }

        /// <summary>
        /// 更新设备类型信息
        /// </summary>
        /// <param name="_receiveObjectT"></param>
        /// <returns></returns>
        [HttpPost("UpdateDeviceType")]
        public async Task<ReturnObject> UpdateDeviceType(ReceiveObjectT<DeviceType> _receiveObjectT)
        {
            /*
            if (id != deviceType.DeviceTypeId)
            {
                return BadRequest();
            }
            */
            ReturnObject _returnObject = new ReturnObject();

            DeviceType deviceType= _receiveObjectT.Data;
            bool isSuccess = await _deviceTypeService.UpdateDeviceTypeAsync(deviceType);
            if (isSuccess)
            {
                _returnObject.Code = 1;
            }
            else
            {
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = "更新设备类型信息失败";
            }
            return _returnObject;
            //return NoContent();
        }

        /// <summary>
        /// 删除设备类型信息
        /// </summary>
        /// <param name="_receiveObjectT"></param>
        /// <returns></returns>
        [HttpPost("DeleteDeviceType")]
        public async Task<ReturnObject> DeleteDeviceType(ReceiveObjectT<int> _receiveObjectT)
        {
            ReturnObject _returnObject = new ReturnObject();

            int id = _receiveObjectT.Data;
            bool isSuccess = await _deviceTypeService.DeleteDeviceTypeAsync(id);
            if (isSuccess)
            {
                _returnObject.Code = 1;
            }
            else
            {
                _returnObject.Code = 0;
                _returnObject.ErrorMsg = "删除设备类型信息失败";
            }
            return _returnObject;
            //  return NoContent();
        }
    }
}